package com.example.administrator.myappjisuanqi;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    private String firstNumber = "";
    private String secondNumber = "";
    private int ch = 0;//
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView text = (TextView) findViewById(R.id.text);
        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        Button button3 = (Button) findViewById(R.id.button3);
        Button button4 = (Button) findViewById(R.id.button4);
        Button button5 = (Button) findViewById(R.id.button5);
        Button button6 = (Button) findViewById(R.id.button6);
        Button button7 = (Button) findViewById(R.id.button7);
        Button button8 = (Button) findViewById(R.id.button8);
        Button button9 = (Button) findViewById(R.id.button9);
        Button button0 = (Button) findViewById(R.id.lin);


        Button jia = (Button) findViewById(R.id.jia);
        Button jian = (Button) findViewById(R.id.jian);
         Button chen = (Button) findViewById(R.id.chen);
        Button chu = (Button) findViewById(R.id.chu);
        Button den = (Button) findViewById(R.id.den);


        Button tuge = (Button) findViewById(R.id.tuige);
        Button shaChu = (Button) findViewById(R.id.shanChu);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "1";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "1";
                    text.setText(secondNumber);
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "2";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "2";
                    text.setText(secondNumber);
                }
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "3";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "3";
                    text.setText(secondNumber);
                }
            }
        });


        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "4";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "4";
                    text.setText(secondNumber);
                }
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "5";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "5";
                    text.setText(secondNumber);
                }
            }

        });

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "6";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "6";
                    text.setText(secondNumber);
                }
            }
        });

        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "7";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "7";
                    text.setText(firstNumber);
                }
            }
        });

        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "8";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "8";
                    text.setText(secondNumber);
                }
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ch == 0) {
                    firstNumber = firstNumber + "9";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "9";
                    text.setText(secondNumber);
                }
            }
        });
        button0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ch == 0) {
                    firstNumber = firstNumber + "0";
                    text.setText(firstNumber);
                } else {
                    secondNumber = secondNumber + "0";
                    text.setText(secondNumber);

                }
            }
        });

        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ch = 1;

            }
        });
        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ch = 2;

            }
        });

        chen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ch = 3;

            }
        });


        chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ch = 4;
            }
        });
        den.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                double a = Double.valueOf(firstNumber.toString());
                double b = Double.valueOf(secondNumber.toString());

                double den = 0;

                switch (ch) {
                    case 1:
                        den = a + b;
                        break;
                    case 2:
                        den = a - b;
                        break;
                    case 3:
                        den = a * b;
                        break;
                    case 4:
                        den = a / b;


                }

                text.setText("结果" + den);


            }
        });
        shaChu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                firstNumber = "";
                secondNumber = "";
                text.setText("");
            }
        });
        tuge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String currentString = text.getText().toString();
                int length = currentString.length();
                String newString = currentString.substring(0, length - 1);
                text.setText(newString);

            }
        });

    }
}
